package com.bgds.sys.socket;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.json.JSONUtil;
import com.bgds.sys.api.bean.mars.dto.DataTimeSerise;
import com.bgds.sys.api.service.DeTongService;
import com.bgds.sys.influx.utils.InfluxUtils;
import com.bgds.sys.module.entity.ClientConfig;
import com.bgds.sys.module.mapper.DeviceMapper;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.nio.ByteBuffer;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author chenchao
 * @date 2021/10/29
 */
@Component
@Slf4j
public class CombustionFanFactory {
    private static final String MSG_KEY = "1201";
    private static final int MSG_LENGTH = 285;
    private static final String INFLUX_TABLE = "speed";
    @Resource
    DeviceMapper deviceMapper;
    @Resource
    private InfluxUtils influxUtils;
    @Resource
    private DeTongService deTongService;

    /**
     * mock假数据
     *
     * @param
     * @return byte[]
     * @author chenchao
     * @date 2021/10/29
     */
    public byte[] mock() {
        ByteBuffer buffer = ByteBuffer.allocate(285);
        buffer.putShort((short) 280);
        buffer.putShort((short) 1201);
        for (int i = 0; i < 70; i++) {
            if (i < 48) {
                buffer.putFloat(NumberUtil.round(Math.random() * 10, new Random().nextInt(4)).floatValue());
            } else {
                buffer.putFloat(0);
            }

        }
        buffer.put((byte) 0x0a);
        return buffer.array();
    }


    /**
     * 从德通接口查询出来助燃风机数据
     *
     * @param
     * @return void
     * @author chenchao
     * @date 2021/10/29
     */
    public byte[] getDetongRestApiData() {
        List<Float> floats = new LinkedList<Float>();
        List<ClientConfig> list = deviceMapper.getCoinfg(MSG_KEY);
        Map<String,  List<ClientConfig>> logs = new HashMap<>();
        List<ClientConfig> log107= new ArrayList<>();
        List<ClientConfig> log101= new ArrayList<>();
        logs.put("107",log107);
        logs.put("101",log101);
        list.forEach(item -> {
            Object value = null;
            List<DataTimeSerise> seriseList;
            if (item.getSeqNumber() < 25) {
                seriseList = deTongService.getObject(item.getRelationId(), 107);
                if (seriseList != null) {
                    value = seriseList.stream().filter(it -> it.getCode().equals("107")).findAny().orElse(null);
                } else {
                    log107.add(item);
                }


            } else {
                seriseList = deTongService.getObject(item.getRelationId(), 101);
                if (seriseList != null) {
                    value = seriseList.stream().filter(it -> it.getCode().equals("101")).findFirst().orElse(null);
                } else {
                    log101.add(item);
                }
            }
            floats.add(value != null ? Double.valueOf((double) value).floatValue() : 0);
            item.setValue(value);
        });
        log.info("德通电文1201数据为空{}",JSONUtil.toJsonStr(logs));
        saveInfluxDB(list.stream().collect(Collectors.toMap(ClientConfig::getEngName, ClientConfig::getValue)));
        return assemblyData(floats);
    }


    /**
     * 装配数据
     *
     * @param
     * @return void
     * @author chenchao
     * @date 2021/10/29
     */
    public byte[] assemblyData(List<Float> list) {
        ByteBuffer buffer = ByteBuffer.allocate(285);
        buffer.putShort((short) MSG_LENGTH);
        buffer.putShort((short) Integer.valueOf(MSG_KEY).intValue());
        list.forEach(item -> {
            buffer.putFloat(item);
        });
        while ((buffer.limit() - buffer.position()) > 1) {
            buffer.putFloat(0);
        }
        buffer.put((byte) 0x0a);
        return buffer.array();
    }


    /**
     * 保存到数据库
     *
     * @param
     * @return void
     * @author chenchao
     * @date 2021/10/29
     */
    public void saveInfluxDB(Map<String, Object> map) {
        influxUtils.insert(INFLUX_TABLE, null, map);

    }
}
